<template>
  <div>
    <el-form :data="user" status-icon ref="user" class="demo-ruleForm">
      <el-descriptions title="用户信息" :column="2" border style="padding-top: 20px">
        <template slot="extra">
          <el-button type="primary" icon="el-icon-edit" circle @click="btnEdit"></el-button>
        </template>
        <el-descriptions-item label="头像" >
          <el-upload
              class="avatar-uploader"
              action="http://localhost:9090/file/upload"
              :headers="headers"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              v-show='hide'
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-upload
              class="avatar-uploader"
              :show-file-list="false"
              v-show='display'
              action="http://localhost:9090/file/upload"
              disabled
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-descriptions-item>
        <el-descriptions-item></el-descriptions-item>
        <el-descriptions-item label="名字">
          <el-input :readonly="read ? false : 'readonly'" style="width: 250px" v-model="user.name"></el-input>
        </el-descriptions-item>
        <el-descriptions-item label="性别">
          <el-radio-group v-model="user.sex" size="mini" :disabled="read ? false : 'disabled'">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-descriptions-item>
        <el-descriptions-item label="用户名">{{user.username}}</el-descriptions-item>
        <el-descriptions-item label="手机号">
          <el-input :readonly="read ? false : 'readonly'" style="width: 250px" v-model="user.phone"></el-input>
        </el-descriptions-item>
        <el-descriptions-item label="角色">
          <el-tag style="margin-left: 3px" size="small" v-for="(item,index) in user.role" :key="index">{{item.name}}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="居住">
          <el-input :readonly="read ? false : 'readonly'" style="width: 250px" v-model="user.city"></el-input>
        </el-descriptions-item>
        <el-descriptions-item label="邮箱">
          <el-input :readonly="read ? false : 'readonly'" style="width: 250px" v-model="user.email"></el-input>
        </el-descriptions-item>
        <el-descriptions-item label="联系地址">
          <el-input :readonly="read ? false : 'readonly'" style="width: 250px" v-model="user.address"></el-input>
        </el-descriptions-item>
      </el-descriptions>
      <el-form-item style="margin-top: 10px">
        <el-button v-show='hide' style="float: right;margin-right: 5px" @click="back">取消</el-button>
        <el-button type="primary" v-show='hide' style="float: right;margin-right: 5px" @click="btnSave">确认修改</el-button>
      </el-form-item>
    </el-form>

<!--    <el-descriptions title="用户信息" :model="user" :column="2" border style="padding-top: 20px">
      <el-descriptions-item label="头像">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:9090/file/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
        >
          <img v-if="user.avatar" :src="user.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-descriptions-item>
      <el-descriptions-item></el-descriptions-item>
      <el-descriptions-item label="名字">{{user.name}}</el-descriptions-item>
      <el-descriptions-item label="性别">男</el-descriptions-item>
      <el-descriptions-item label="用户名">{{user.username}}</el-descriptions-item>
      <el-descriptions-item label="手机号">{{user.phone}}</el-descriptions-item>
      <el-descriptions-item label="角色">
          <el-tag style="margin-left: 3px" size="small" v-for="(item,index) in user.role" :key="index">{{item.name}}</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="居住">{{user.city}}</el-descriptions-item>
      <el-descriptions-item label="邮箱">{{user.email}}</el-descriptions-item>
      <el-descriptions-item label="联系地址">{{user.address}}</el-descriptions-item>
    </el-descriptions>-->
  </div>
</template>

<script>
export default {
  name: "Person",
  data(){
    return{
      user:[],
      read: 0,
      userForm:[],
      hide: false,
      display: true,
    }
  },
  computed: {
    headers() {
      return {
        "Authorization": localStorage.getItem("token")
      }
    }
  },
  created() {
    this.getUser()
  },
  methods:{
    getUser() {
      this.request.get("/user/username").then(res => {
        console.log(res.data.data)
        this.user = res.data.data
      })
    },
    handleAvatarSuccess(res){
      this.user.avatar = res
    },
    //修改按钮
    btnEdit(){
      this.read = 1
      this.hide = true
      this.display = false
      this.userForm = this.user
    },
    //保存按钮
    btnSave(){
      this.request.post("/user/updateUser",this.user).then(res => {
        if (res.data.code === '200'){
          this.$message.success("保存成功")
          this.$emit("refreshUser")
          this.getUser()
          this.hide = false
          this.display = true
          this.read = 0
        }else {
          this.$message.error("保存失败")
        }
      })
    },
    //取消按钮
    back(){
      this.getUser()
      this.hide = false
      this.display = true
      this.read = 0
    }
  }
}
</script>

<style>
.avatar-uploader {
  text-align: left;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
.el-descriptions__body .el-descriptions__table .el-descriptions-item__cell{
  padding-left: 20px;
}
.el-descriptions-item__cell.el-descriptions-item__content{
  width: 300px;
  height: 41px;
}

.el-descriptions__body .el-descriptions__table .el-descriptions-item__label.is-bordered-label {
  text-align: center;
}
.el-descriptions--mini, .el-descriptions--small{
  font-size: 16px;
}
.el-input__inner{
  border : 0px;
  font-size: 16px;
  padding-left: 0px;
}
</style>